package com.fivebrms.hibernate.entity.dao;

import java.util.Collection;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;

import com.fivebrms.hibernate.entity.model.ConfidentialityForm;
import com.fivebrms.hibernate.entity.model.W9Form;

public class ConfidentialityFormDaoHibernate implements ConfidentialityDAO
{
	final Logger	       log	= LoggerFactory.getLogger(ConfidentialityFormDaoHibernate.class);

	private SessionFactory	sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory)
	{
		this.sessionFactory = sessionFactory;
	}

	public SessionFactory getSessionFactory()
	{
		return this.sessionFactory;
	}

	public Collection<ConfidentialityForm> findAll()
	{
		Session session = SessionFactoryUtils.getSession(getSessionFactory(), true);
		try
		{
			log.info("start ConfidentialityForm find all..");

			Collection<ConfidentialityForm> confidentialityForms = session.createQuery("from ConfidentialityForm").list();

			log.info("end ConfidentialityForm find all..");

			return confidentialityForms;
		}
		catch (HibernateException e)
		{
			throw SessionFactoryUtils.convertHibernateAccessException(e);
		}
	}

	public void createConfidentialityForm(ConfidentialityForm confidentialityForm) throws DataAccessException
	{
		log.info("..1..createConfidentialityForm .." + confidentialityForm);
		log.info("..2..createConfidentialityForm.." + this.getSessionFactory());
		Session session = SessionFactoryUtils.getSession(this.getSessionFactory(), true);
		log.info("..3..createW9Form..->" + session);
		session.save(confidentialityForm);
		log.info("..4..createConfidentialityForm.." + confidentialityForm);
	}

	public void deleteConfidentialityForm(ConfidentialityForm confidentialityForm) throws DataAccessException
	{
		Session session = SessionFactoryUtils.getSession(this.getSessionFactory(), true);
		session.delete(confidentialityForm);
	}

	@Override
	public void updateConfidentialityForm(ConfidentialityForm confidentialityForm) throws DataAccessException
	{
		Session session = SessionFactoryUtils.getSession(this.getSessionFactory(), true);
		session.update(confidentialityForm);
	}

}
